package com.ant.common.result;

import com.ant.common.constant.RequestConstant;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @author LB
 */
@Data
@Slf4j
public class ResponseResult implements Serializable {

    private boolean successful;
    private Object result;
    private String msg;
    private String type;
    private int code;
    private long count;

    /**
     * 默认返回成功
     */
    public ResponseResult() {
        this.successful = true;
        this.result = null;
        this.msg = "";
        this.type = RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName();
        this.code = RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode();
        this.count = 0;
    }

    public static ResponseResult success(String msg){
        return new ResponseResult(true, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), null, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult success(Object resultData){
        return new ResponseResult(true, "", RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), resultData, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult success(String msg, int code, long count){
        return new ResponseResult(true, msg, code, null, count, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult success(String msg, int code){
        return new ResponseResult(true, msg, code, null, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult success(String msg, long count){
        return new ResponseResult(true, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), null, count, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult success(String msg, Object resultData){
        return new ResponseResult(true, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), resultData, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult success(String msg, int code, Object resultData){
        return new ResponseResult(true, msg, code, resultData, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult success(String msg, Object resultData, long count){
        return new ResponseResult(true, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), resultData, count, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult fail(String msg){
        return new ResponseResult(false, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_2.getCode(), null, 0, RequestConstant.WRAPPED_RESULT_TYPE.ERROR.getName());
    }

    public static ResponseResult fail(String msg, int code){
        return new ResponseResult(false, msg, code, null, 0, RequestConstant.WRAPPED_RESULT_TYPE.ERROR.getName());
    }

    public static ResponseResult fail(String msg, int code, Object resultData){
        return new ResponseResult(false, msg, code, resultData, 0, RequestConstant.WRAPPED_RESULT_TYPE.ERROR.getName());
    }

    public static ResponseResult warn(String msg){
        return new ResponseResult(false, msg, 0, null, 0, RequestConstant.WRAPPED_RESULT_TYPE.WARN.getName());
    }

    public ResponseResult(boolean successful, String msg, int code, Object result, long count, String type){
        this.successful = successful;
        this.msg = msg;
        this.code = code;
        this.result = result;
        this.count = count;
        this.type = type;

        if (!successful){
            Throwable t = new Throwable();
            StackTraceElement[] stackTraceElements = t.getStackTrace();

            String fileName = "";
            String methodName = "";

            //查找调用过来的类或者方法
            for (StackTraceElement stackTraceElement :stackTraceElements) {
                if (stackTraceElement.getClassName().contains("com.canteen") &&
                    stackTraceElement.getClassName().contains("controller")){
                    fileName = stackTraceElement.getFileName();
                    methodName = stackTraceElement.getMethodName();
                    break;
                }
            }
            log.error("ResponseResult failed from path:[{}], method:[{}], error message:[{}]", fileName, methodName, msg);
        }
    }

}
